﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.UI.Template;
using LEARNREN.COM.BLL.Enums;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 留言列表标签
    /// </summary>
    class MSGLLabel : ListLabelBase
    {
        public MSGLLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);
                string header = LabelProviderHelper.GetListLabelHeader(this.LabelString);
                string item = LabelProviderHelper.GetListLabelItem(this.LabelString);
                string footer = LabelProviderHelper.GetListLabelFooter(this.LabelString);
                string empty = LabelProviderHelper.GetListLabelEmpty(this.LabelString);
                // 总记录数
                int total = 0;
                // 符合条件的记录
                List<Message> messages = null;

                // 栏目Id
                long cid = 0;
                string cidStr = paras[0].ToLower();
                if (cidStr == "cur")
                {
                    cid = this.Context.CatalogueID;
                }
                else
                {
                    if (!long.TryParse(cidStr, out cid))
                    {
                        return this.LabelString;
                    }
                }

                // 页面大小，默认为10
                int pageSize = 0;
                if (!int.TryParse(paras[2], out pageSize))
                {
                    pageSize = 10;
                }

                // 标题字数
                int titleLen = 0;
                if (!int.TryParse(paras[3], out titleLen))
                {
                    return this.LabelString;
                }

                bool showPager = paras[4].ToLower() == "1";

                // 是否显示子栏目工作
                bool showDescendantsMessages = paras[5] == "1";

                // 页码以及留言列表查询
                int page = 0;
                string pageStr = paras[1].ToLower();
                if (this.Context.Container is CatalogueTemplate)
                {
                    if (pageStr == "cur")
                    {
                        page = (this.Context.Container as CatalogueTemplate).curPage;
                    }
                    else
                    {
                        if (!int.TryParse(pageStr, out page))
                        {
                            return this.LabelString;
                        }
                    }

                    Catalogue c = this.Context.State as Catalogue;
                    total = c.ContentCount;
                    if (showDescendantsMessages)
                    {
                        messages = this.GroupLogicFactory.MessageLogic.GetListByPage(p => p.ParentPath != null && p.ParentPath.StartsWith(c.Path),
                                                                                        p => p.CreatedTime,
                                                                                        OrderEnum.Descending,
                                                                                        pageSize,
                                                                                        page);
                    }
                    else
                    {
                        messages = this.GroupLogicFactory.MessageLogic.GetListByPage(p => p.CatalogueId == cid,
                                                                                    p => p.CreatedTime,
                                                                                    OrderEnum.Descending,
                                                                                    pageSize,
                                                                                    page);
                    }

                }
                else
                {
                    if (!int.TryParse(pageStr, out page))
                    {
                        return this.LabelString;
                    }
                    if (showDescendantsMessages)
                    {
                        Catalogue c = this.GroupLogicFactory.CatalogueLogic.GetByIdFromCache(cid);
                        messages = this.GroupLogicFactory.MessageLogic.GetListByPage(out total,
                                                                                        p => p.ParentPath != null && p.ParentPath.StartsWith(c.Path),
                                                                                        p => p.CreatedTime,
                                                                                        OrderEnum.Descending,
                                                                                        pageSize,
                                                                                        page);
                    }
                    else
                    {
                        Catalogue c = this.GroupLogicFactory.CatalogueLogic.GetByIdFromCache(cid);
                        messages = this.GroupLogicFactory.MessageLogic.GetListByPage(out total,
                                                                                        p => p.CatalogueId == cid,
                                                                                        p => p.CreatedTime,
                                                                                        OrderEnum.Descending,
                                                                                        pageSize,
                                                                                        page);
                    }
                }

                if (messages.Count == 0)
                {
                    return empty;
                }

                // 组装
                StringBuilder sb = new StringBuilder();
                if (string.IsNullOrEmpty(header) && string.IsNullOrEmpty(item) && string.IsNullOrEmpty(footer))
                {
                    sb.AppendLine("<ul class=\"msg_list\">");
                    foreach (var msg in messages)
                    {
                        sb.Append("<li class=\"msg_item\">");
                        sb.AppendFormat("<span class=\"title\"><a href=\"{0}\" title=\"{1}\" target=\"_blank\">{2}</a></span><span class=\"datetime\">{3}</span>",
                            this.GroupLogicFactory.MessageLogic.GetURL(msg),
                            msg.MsgTitle,
                            msg.MsgTitle.ToShortFormat(titleLen),
                            msg.CreatedTime);
                        sb.AppendLine("</li>");
                    }
                    sb.AppendLine("</ul>");
                }
                else
                {
                    sb.AppendLine(header);


                    TemplateContext temContext = new TemplateContext();
                    temContext.CatalogueID = cid;
                    temContext.Container = this;
                    temContext.TemplateAbsoluteDirectory = this.Context.TemplateAbsoluteDirectory;
                    temContext.TemplateVirtualDirectory = this.Context.TemplateVirtualDirectory;
                    foreach (var article in messages)
                    {
                        StringBuilder sbitem = new StringBuilder(item);
                        temContext.CatalogueID = article.CatalogueId;
                        temContext.State = article;
                        List<ILabelProvider> childLabels = LabelContenerHelper.GetLabels(item, this.GroupLogicFactory, temContext);
                        foreach (var label in childLabels)
                        {
                            if (label is MSGPLabel)
                            {
                                string lhtml = label.GetHtml();
                                if (lhtml != label.LabelString)
                                {
                                    sbitem = sbitem.Replace(label.LabelString, lhtml);
                                }
                            }
                        }
                        sb.AppendLine(sbitem.ToString());
                    }

                    sb.AppendLine(footer);
                }
                if (showPager)
                {
                    sb.AppendLine(GetPager(page, pageSize, total));
                }

                return sb.ToString();
            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
